package com.example.shuiyin.cli;

import com.example.shuiyin.service.MinioStorageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
@Profile("!prod") // 仅在非生产环境启用
public class CleanupCommand implements CommandLineRunner {

    private final MinioStorageService minioStorageService;

    @Override
    public void run(String... args) {
        // 检查是否有清理命令
        for (int i = 0; i < args.length; i++) {
            if ("--cleanup-test".equals(args[i])) {
                String prefix = null;
                if (i + 1 < args.length && !args[i+1].startsWith("--")) {
                    prefix = args[i+1];
                }
                
                log.info("通过命令行开始清理测试文件，前缀: {}", prefix);
                int deletedCount;
                
                if (prefix != null) {
                    deletedCount = minioStorageService.cleanupTestFiles(prefix);
                } else {
                    deletedCount = minioStorageService.cleanupAllTestFiles();
                }
                
                log.info("命令行清理测试文件完成，共删除{}个文件", deletedCount);
                break;
            }
        }
    }
} 